package com.returnsoft.softphone.eao;

import java.io.Serializable;
import java.util.List;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import com.returnsoft.softphone.entity.SessionType;
import com.returnsoft.softphone.exception.EaoExcepcion;

@LocalBean
@Stateless
public class SessionTypeEao implements Serializable{
	
	private static final long serialVersionUID = 1L;

	@PersistenceContext(name = "CCenterEJB", unitName = "CCenterEJB")
	private EntityManager em;
	
	public List<SessionType> findAll()
			throws EaoExcepcion {
		try {
			TypedQuery<SessionType> q = em
					.createQuery(
							"SELECT st FROM SessionType st",
							SessionType.class);
			return q.getResultList();
		} catch (Exception e) {
			e.printStackTrace();
			throw new EaoExcepcion(e.getMessage());
		}
	}

	public List<SessionType> findType(Integer campaignId)
			throws EaoExcepcion {
		try {
			TypedQuery<SessionType> q = em
					.createQuery(
							"SELECT st FROM SessionType st left join st.campaigns c WHERE c.id = :campaignId",
							SessionType.class);
			q.setParameter("campaignId", campaignId);
			return q.getResultList();
		} catch (Exception e) {
			e.printStackTrace();
			throw new EaoExcepcion(e.getMessage());
		}
	}

	public SessionType findType(String code) throws EaoExcepcion {
		try {
			TypedQuery<SessionType> q = em.createQuery(
					"SELECT st FROM SessionType st WHERE st.code = :code",
					SessionType.class);
			q.setParameter("code", code);
			return q.getSingleResult();
		} catch (NoResultException e) {
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			throw new EaoExcepcion(e.getMessage());
		}
	}
	
	public SessionType findTypeById(Integer sessionTypeId) throws EaoExcepcion {
		try {
			TypedQuery<SessionType> q = em.createQuery(
					"SELECT st FROM SessionType st WHERE st.id = :sessionTypeId",
					SessionType.class);
			q.setParameter("sessionTypeId", sessionTypeId);
			return q.getSingleResult();
		} catch (NoResultException e) {
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			throw new EaoExcepcion(e.getMessage());
		}
	}
	
	

}
